package com.gxuwz.medical.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Method;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gxuwz.medical.domain.menu.Menu;
import com.gxuwz.medical.domain.user.User;
import com.gxuwz.medical.vo.PageBean;

public class UserServlet extends BaseServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		String url = req.getRequestURI();
		String methodName = url.substring(url.lastIndexOf("/") + 1,
				url.lastIndexOf("."));
		Method method = null;

		try {
			method = getClass().getDeclaredMethod(methodName,
					HttpServletRequest.class, HttpServletResponse.class);
			method.invoke(this, req, resp);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	
	public void jump(HttpServletRequest request, HttpServletResponse response,String path) throws ServletException, IOException, SQLException{
		User u = new User();
		String page = request.getParameter("page");
		if (page == null)
			page = "1";
		int currentpage = Integer.parseInt(page);
		PageBean pageBean = u.queryAll(currentpage, 5);
		request.setAttribute("pageBean", pageBean);
		request.getRequestDispatcher(path).forward(request, response);
	}
	
	public void list(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException, SQLException {
		String path="page/user/list.jsp";
		User u = new User();
		String page = request.getParameter("page");
		if (page == null)
			page = "1";
		int currentpage = Integer.parseInt(page);
		PageBean pageBean = u.queryAll(currentpage, 5);
		request.setAttribute("pageBean", pageBean);
		request.getRequestDispatcher(path).forward(request, response);
	}

	public void add(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException, SQLException {
		String path = "list.jsp";
		String userid = request.getParameter("userid");
		String pwd = request.getParameter("pwd");
		String fullName = request.getParameter("fullName");
		String[] roleid = request.getParameterValues("roleid");
		User u = new User();
		u.setUserid(userid);
		u.setPwd(pwd);
		u.setFullName(fullName);
		u.addRole(roleid);
		u.add();
		jump(request,response,path);
	}

	public void del(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException, SQLException {
		String path = "page/user/list.jsp";
		String userid = request.getParameter("userid");
		userid = new String(userid.getBytes("ISO-8859-1"),"UTF-8");
		System.out.println(userid+"test");
		User u = new User();
		u.delete(userid);
		jump(request,response,path);
	}

	public void edit(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException, SQLException {
		String path = "list.jsp";
		String userid = request.getParameter("userid");
		String pwd = request.getParameter("pwd");
		String fullName = request.getParameter("fullName");
		String[] roleid = request.getParameterValues("roleid");
		User u = new User();
		u.setUserid(userid);
		u.setPwd(pwd);
		u.setFullName(fullName);
		u.addRole(roleid);
		u.update();
		response.setCharacterEncoding("utf-8");
		jump(request,response,path);
	}
}
